package com.projectxxxxxxxxxx.widget;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.widget.RemoteViews;

import com.projectxxxxxxxxxx.Constants;
import com.projectxxxxxxxxxx.R;
import com.projectxxxxxxxxxx.animator.Animator;
import com.projectxxxxxxxxxx.animator.DoorAnimator;
import com.projectxxxxxxxxxx.info.Info;
import com.projectxxxxxxxxxx.screens.HardwareScreen.LabelParams;

public class WidgetView {

    private static boolean isUSBConnected = false;
    private static boolean isPOWERConnected = false;
    private static boolean isSDConnected = false;
    private static boolean isHeadsetConnected = false;
    
    public static boolean isUpdateActionLight = false;
    public static boolean isFirdtUpdateActionLight = false;
    

    public static Animator animator;

    private static int actionNumber = 1;

    private static String androidVersion = "";
    private static LabelParams label;
    
    public static boolean helpOneVisible = true;
    public static boolean helpTwoVisible = true;

	public static int EYES = 0;
	public static int EYES_VERTICAL = 0;


    public RemoteViews render(Context context, final RemoteViews views, int id) {

    	if(!helpOneVisible){
	        Intent intentDoor = new Intent(WidgetManager.ACTION_WIDGET_OPEN_DOOR);
	        PendingIntent pendingIntentDoor = PendingIntent.getBroadcast(context, id, intentDoor, 0);
	        views.setOnClickPendingIntent(R.id.open_door_middle, pendingIntentDoor);
	
	        Intent intentDoorOpen = new Intent(WidgetManager.ACTION_WIDGET_CLOSE_DOOR);
	        PendingIntent pendingIntentDoorOpen = PendingIntent.getBroadcast(context, id, intentDoorOpen, 0);
	        views.setOnClickPendingIntent(R.id.open_door_end, pendingIntentDoorOpen);
    	}

//        Intent intentPrev = new Intent(WidgetManager.ACTION_WIDGET_CHANGE_SCREEN_TO_PREV);
//        PendingIntent pendingIntentPrev = PendingIntent.getBroadcast(context, id, intentPrev, 0);
//        views.setOnClickPendingIntent(R.id.screenprev, pendingIntentPrev);
//
//        Intent intentNext = new Intent(WidgetManager.ACTION_WIDGET_CHANGE_SCREEN_TO_NEXT);
//        PendingIntent pendingIntentNext = PendingIntent.getBroadcast(context, id, intentNext, 0);
//        views.setOnClickPendingIntent(R.id.screennext, pendingIntentNext);

        Intent intentHelpOne = new Intent(Constants.ACTION_HELP_CLOSE);
        PendingIntent pendingIntentHelpOne = PendingIntent.getBroadcast(context, id, intentHelpOne, 0);
        views.setOnClickPendingIntent(R.id.robo_help_close_one, pendingIntentHelpOne);
        
        Intent intentHelpTwo = new Intent(Constants.ACTION_HELP_CLOSE);
        PendingIntent pendingIntentHelpTwo = PendingIntent.getBroadcast(context, id, intentHelpTwo, 0);
        views.setOnClickPendingIntent(R.id.robo_help_close_two, pendingIntentHelpTwo);
        
        if (isUSBConnected) {
            views.setViewVisibility(R.id.robo_usb_on, View.VISIBLE);
        } else {
        	views.setViewVisibility(R.id.robo_usb_on, View.GONE);
        }

        if (isPOWERConnected) {
        	views.setViewVisibility(R.id.robo_power_on, View.VISIBLE);
        } else {
        	views.setViewVisibility(R.id.robo_power_on, View.GONE);
        }
        
        if (hasStorage()) {
        	views.setViewVisibility(R.id.robo_sd_on, View.VISIBLE);
        } else {
        	views.setViewVisibility(R.id.robo_sd_on, View.GONE);
        }

        if (isHeadsetConnected) {
        	views.setViewVisibility(R.id.robo_headphones_on, View.VISIBLE);
        } else {
        	views.setViewVisibility(R.id.robo_headphones_on, View.GONE);
        }
        
        if (helpOneVisible) {
            views.setViewVisibility(R.id.robo_help_one, View.VISIBLE);
        } else {
            views.setViewVisibility(R.id.robo_help_one, View.GONE);
        }

        if (WidgetView.helpTwoVisible && !WidgetView.helpOneVisible &&
                DoorAnimator.openDoor) {
            views.setViewVisibility(R.id.robo_help_two, View.VISIBLE);
        } else {
            views.setViewVisibility(R.id.robo_help_two, View.GONE);
        }
        
        renderLabel(views);

        return views;
    }

    private void renderLabel(final RemoteViews views) {

        if (TextUtils.isEmpty(androidVersion)) {
            androidVersion = "Android " + Info.SYSTEM.get().get(Info.INFO_ANDROID_VERSION);
        }

        if (label == null) {
            for (LabelParams type : LabelParams.values()) {
                if (type.getName().equals(androidVersion)) {
                    label = type;
                    break;
                }
            }
        }

        views.setImageViewResource(R.id.robo_label_2, label.getimageResId());
    }

    private RemoteViews updateActionLight(final RemoteViews views){
    	
    	switch(actionNumber){
    	case 1:
        	actionNumber = checkParameters();
        	if(actionNumber == 2){
        		views.setViewVisibility(R.id.robo_action_light_blue, View.VISIBLE);
        	} else {
        		views.setViewVisibility(R.id.robo_action_light_red, View.VISIBLE);
        	}
    		break;
    	case 2:
    		views.setViewVisibility(R.id.robo_action_light_blue, View.GONE);
    		actionNumber = 1;
    		break;
    	case 3:
    		views.setViewVisibility(R.id.robo_action_light_red, View.GONE);
    		actionNumber = 1;
    		break;
    	}
    	return views;
    }    
    
	public void updateActionEyes(final RemoteViews views) {

		switch (EYES_VERTICAL) {
		case 0:
			switch (EYES) {
			case 0:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_0);
				break;
			case 1:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_l1);
				break;
			case -1:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_r1);
				break;
			case 2:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_l2);
				break;
			case -2:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_r2);
				break;
			case 3:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_l3);
				break;
			case -3:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_r3);
				break;
			case 4:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_l4);
				break;
			case -4:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_r4);
				break;
			}
			break;
			
		case -1:
				switch (EYES) {
				case 0:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_d_0);
					break;
				case 1:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_d_l1);
					break;
				case -1:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_d_r1);
					break;
				case 2:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_d_l2);
					break;
				case -2:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_d_r2);
					break;
				case 3:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_d_l3);
					break;
				case -3:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_d_r3);
					break;
				case 4:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_d_l4);
					break;
				case -4:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_d_r4);
					break;
				}
			break;
			
		case 1:
			switch (EYES) {
			case 0:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_u_0);
				break;
			case 1:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_u_l1);
				break;
			case -1:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_u_r1);
				break;
			case 2:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_u_l2);
				break;
			case -2:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_u_r2);
				break;
			case 3:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_u_l3);
				break;
			case -3:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_u_r3);
				break;
			case 4:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_u_l4);
				break;
			case -4:
				views.setImageViewResource(R.id.robo_eyes, R.drawable.eyes_u_r4);
				break;
			}
			break;
		default:
			break;
		}
	}

    private int checkParameters(){
    	
    	String cpuUsage = Info.CPU_USAGE.get().get(Info.INFO_CPU_USAGE);
        if(Constants.CRITICAL_VALUE_CPU_USAGE < Integer.parseInt(cpuUsage)){
        	return 3;
        }
        
        String ramUsage = Info.MEMORY_USAGE.get().get(Info.INFO_MEMORY_USAGE);
        if(Constants.CRITICAL_VALUE_RAM_USAGE < Integer.parseInt(ramUsage)){
        	return 3;
        }
        
        String internalUsage = Info.STORAGE_INTERNAL_USAGE.get().get(Info.INFO_STORAGE_INTERNAL_USAGE);
        if(Constants.CRITICAL_VALUE_INTERNAL_USAGE < Integer.parseInt(internalUsage)){
        	return 3;
        }
        String externalUsage = Info.STORAGE_EXTERNAL_USAGE.get().get(Info.INFO_STORAGE_EXTERNAL_USAGE);
        if(Constants.CRITICAL_VALUE_EXTERNAL_USAGE < Integer.parseInt(externalUsage)){
        	return 3;
        }
        
        String battery = Info.BATTERY.get().get(Info.INFO_BATERRY_LAVEL);
        if(Constants.CRITICAL_VALUE_BATTERY_USAGE > Integer.parseInt(battery)){
        	return 3;
        }
        
    	return 2;
    }
    
    public RemoteViews renderActionLight(Context context, RemoteViews views, final boolean ind) {
        isUpdateActionLight = ind;
        views = updateActionLight(views);
        isUpdateActionLight = false;
        return views;
    }
    
    public void renderHeadsetIcon(final boolean ind) {
        isHeadsetConnected = ind;
    }
    
    public void renderSDIcon(final boolean ind) {
        isSDConnected = hasStorage();
    }
    
    public void renderUSBIcon(final boolean ind) {
        isUSBConnected = ind;
    }

    public void renderPOWERIcon(final boolean ind) {
        isPOWERConnected = ind;
    }

    public void renderAnimation(final Animator animator) {
        this.animator = animator;
    }
    
    public void animate(Context context, final RemoteViews views) {
        if (animator != null) {
            animator.animate(views, label);
        }
    }

    public static boolean hasStorage() {  
        String state = Environment.getExternalStorageState();  
      
        if (Environment.MEDIA_MOUNTED.equals(state)) {  
            return true;  
        }  
        return false;  
    }  
    
}
